package com.wmh.baseservice.robot.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.web.UserUtil;
import com.wmh.baseservice.robot.entity.RobotTaskAdPushUnit;
import com.wmh.baseservice.robot.enums.RobotPushTaskType;
import com.wmh.baseservice.robot.pojo.vo.robottaskadpushunit.RobotTaskAdPushUnitCreateReqVO;
import com.wmh.baseservice.robot.pojo.vo.robottaskadpushunit.RobotTaskAdPushUnitPageReqVO;
import com.wmh.baseservice.robot.pojo.vo.robottaskadpushunit.RobotTaskAdPushUnitPushTaskReqVO;
import com.wmh.baseservice.robot.pojo.vo.robottaskadpushunit.RobotTaskAdPushUnitUpdateReqVO;
import com.wmh.baseservice.robot.service.RobotTaskAdPushUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.stream.Collectors;

import static com.wmh.baseservice.common.utils.returnbody.ReturnBody.success;

/**
 * @author 谢骏
 * @date 2023/9/8 10:05
 **/
@Validated
@RestController
@Api(tags = "机器任务管理员发布单元接口")
@RequestMapping("robot/ad-push-unit")
@Slf4j
public class RobotTaskAdPushUnitController {

    @Resource
    private RobotTaskAdPushUnitService robotTaskAdPushUnitService;

    @Resource
    private UserUtil userUtil;

    @PostMapping("create")
    @ApiOperation(value = "创建")
    public String create(@Valid @RequestBody RobotTaskAdPushUnitCreateReqVO reqVO) {
        robotTaskAdPushUnitService.create(reqVO);
        return success();
    }

    @PostMapping("update")
    @ApiOperation(value = "修改")
    public String update(@Valid @RequestBody RobotTaskAdPushUnitUpdateReqVO reqVO) {
        robotTaskAdPushUnitService.update(reqVO);
        return success();
    }

    @PostMapping("delete")
    @ApiModelProperty(value = "删除")
    public String delete(@NotNull(message = "未提交主键") Long id) {
        robotTaskAdPushUnitService.removeById(id);
        return success();
    }

    @GetMapping("getParam")
    @ApiOperation(value = "获取指定单位任务参数")
    public String getParam(@NotNull(message = "未提交主键") Long id) {
        return success(robotTaskAdPushUnitService.getParam(id));
    }

    @GetMapping("options")
    @ApiOperation(value = "选项")
    public String options(Boolean lookMe) {
        boolean superAdmin = userUtil.isSuperAdmin();
        return success(robotTaskAdPushUnitService.list(new LambdaQueryWrapperX<RobotTaskAdPushUnit>()
                .eq(superAdmin && lookMe != null && lookMe, RobotTaskAdPushUnit::getAdId, userUtil.getId())
                .eq(!superAdmin, RobotTaskAdPushUnit::getAdId, userUtil.getId())
                .select(RobotTaskAdPushUnit::getId, RobotTaskAdPushUnit::getName, RobotTaskAdPushUnit::getAdId)
        ));
    }

    @ApiOperation("发布任务类型选项")
    @GetMapping("typeOptions")
    public String typeOptions() {
        return success(
                Arrays.stream(RobotPushTaskType.values()).map(item ->
                        JSONObject.of("label", item.getLabel(), "value", item.getValue())
                ).collect(Collectors.toList())
        );
    }

    @GetMapping("page")
    @ApiOperation(value = "分页")
    public String page(@Valid RobotTaskAdPushUnitPageReqVO reqVO) {
        PageResult<RobotTaskAdPushUnit> pageResult = new PageUtil<RobotTaskAdPushUnit>(reqVO).startPage((page, queryWrapper) -> {
            LambdaQueryWrapper<RobotTaskAdPushUnit> l = queryWrapper.lambda();
            l.like(StrUtil.isNotBlank(reqVO.getName()), RobotTaskAdPushUnit::getName, reqVO.getName());
            l.eq(reqVO.getId() != null, RobotTaskAdPushUnit::getId, reqVO.getId());
            l.eq(reqVO.getTaskId() != null, RobotTaskAdPushUnit::getRobotTaskId, reqVO.getTaskId());
            if (userUtil.isSuperAdmin()) {
                // 查看所有人
                if (reqVO.getLookMe() == null || BooleanUtil.isFalse(reqVO.getLookMe())) {
                    l.eq(reqVO.getAdId() != null, RobotTaskAdPushUnit::getAdId, reqVO.getAdId());
                } else {
                    // 只看自己
                    l.eq(RobotTaskAdPushUnit::getAdId, userUtil.getId());
                }
            } else {
                // 非超级管理员只能看自己
                l.eq(RobotTaskAdPushUnit::getAdId, userUtil.getId());
            }
            robotTaskAdPushUnitService.page(page, queryWrapper);
        });
        pageResult.setList(CollUtil.map(pageResult.getList(), item -> item.setParam(null), true));
        return success(pageResult);
    }

    @PostMapping("pushTask")
    @ApiOperation(value = "发布任务")
    public String pushTask(@Valid @RequestBody RobotTaskAdPushUnitPushTaskReqVO reqVO) {
        robotTaskAdPushUnitService.pushTask(reqVO);
        return success();
    }
}
